package com.itheima.service.impl;

import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.resources.DateUtils;
import com.itheima.service.OrderService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(timeout = 5000)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;




    @Override
    public Order addOrder(Map map) {

        //todo:模拟服务器卡爆  模拟正常运行时需要注释掉
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        //创建一个Order对象
        Order order=new Order();

        //获取日期,并查看当前日期是否已经预约满  // yyyy-MM-dd
        String  orderDate =(String) map.get("orderDate");

        //拼接
        String  orderDate2 =  orderDate + " 09:00:00";

        //使用工具类转换日期时间
        Date date = DateUtils.parseString2Date(orderDate2, "yyyy-MM-dd HH:mm:ss");
        //调用持久层查看是否已经预约满

        Date date1 = DateUtils.parseString2Date(orderDate, "yyyy-MM-dd");

        int count = orderSettingMapper.isOrderSettingOk(date1);
        if (count!=1){
            throw new RuntimeException("当前预约人数已满,请更换预约日期");
        }

        //新老用户判断,根据手机号查询用户表中是否有此用户
        String telephone = (String) map.get("telephone");

        //查询用户表
        Member member=findMemberByTelephone(telephone);

        //判断查询结果
        if (member==null){
            //如果是新客户,将其添加到客户表中
            String idCard = (String) map.get("idCard");
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            //获取当前系统日期
            Date regDate = new Date(System.currentTimeMillis());
            member=new Member();
            member.setName(name);
            member.setSex(sex);
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setRegTime(regDate);
            //调用持久层对象添加进数据库,添加完成后,主键返回到member对象中
            memberMapper.addMember(member);

            //添加订单表
            order=createOrder(member,order,map);
        }else{
            //如果是老客户,判断其是否已经预约过当前日期
            //获取member的id值
            Integer memberId = member.getId();
            //获取套餐id
            String setmealId = (String) map.get("setmealId");

            //查询订单表
            int orderCount=orderMapper.findOrderIsExistByDateAndSetmealIdAndmemberId(memberId,date,setmealId);

            //判断
            if (orderCount!=0){
                //不等于0,说明已预约过
                throw new RuntimeException("请勿重复预约");
            }else{
                //添加预约信息
                order = createOrder(member, order, map);
            }

        }
        return order;
    }

    /***
     * 预约成功回显预约消息
     * @param id 订单id
     * @return 数据集合
     */
    @Override
    public Map orderSuccess(String id) {
        Map map=orderMapper.findOrderById(id);
        return map;
    }

    /*创建订单信息*/
    private Order createOrder(Member member, Order order, Map map) {
        //给Order对象赋值
        order.setMemberId(member.getId());//对应的用户id
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//对应的套餐id

        String orderDate = (String) map.get("orderDate") + " 09:00:00";

        Date date = DateUtils.parseString2Date(orderDate, "yyyy-MM-dd HH:mm:ss");

        order.setOrderDate(date);//订单的预约日期
        order.setOrderType((String) map.get("orderType"));//订单类型
        order.setOrderStatus("未到诊");//预约状态

        order.setIsPaid((Integer) map.get("isPaid"));//是否支付
        order.setOrderNum((String) map.get("orderNum"));//订单编号
        order.setPrice((Integer) map.get("totalPay"));//价格
        order.setCodeUrl((String) map.get("code_url"));
        order.setTelephone((String)map.get("telephone"));
        //调用持久层添加订单
        orderMapper.createOrder(order);

        return order;
    }

    /*更新订单状态*/
    @Override
    public void updateState(Map paramMap) {
        //1.获取订单号
        String oid = (String) paramMap.get("out_trade_no");
        System.out.println("oid = " + oid);// 1000001417925063
        //2.更新订单状态,通过订单号
        orderMapper.updateState(oid);

    }

    /*查询订单支付状态*/
    @Override
    public Order findIsPaidByOrderId(String orderId) {
        return orderMapper.findIsPaidByOrderId(orderId);
    }



    private Member findMemberByTelephone(String telephone) {
        return memberMapper.findMemberByTelephone(telephone);
    }
}
